package com.senven.api.controller;

import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.pojo.OrganAffair;
import com.senven.api.pojo.OrganUser;
import com.senven.api.service.OrganUserService;
import com.senven.api.utils.ReponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LQP
 */
@RequestMapping("/api/organ/user")
@Controller
public class OrganUserController {
    @Autowired
    private OrganUserService organUserService;


    @GetMapping("/getOrganUserAll")
    @ResponseBody
    public ResponsePage<OrganUser> queryOrganUserListAll(@RequestParam(value = "current",defaultValue = "1")int current,
                                                      @RequestParam(value = "size", defaultValue = "10")int size){
        return organUserService.queryOrganUserListAll(current,size);
    }

    /* ID获取社团会员
     * @param oid
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrganUserByOid")
    @ResponseBody
    public ResponsePage<OrganUser> queryOrganUserList(@RequestParam("oid")int oid,
                                                      @RequestParam(value = "current",defaultValue = "1")int current,
                                                      @RequestParam(value = "size", defaultValue = "10")int size){
        return organUserService.queryOrganUserList(oid,current,size);
    }

    /**
     * 姓名模糊查询
     * @param name
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrganUserByName")
    @ResponseBody
    public  ResponsePage<OrganUser> queryOrganUserListByName(@RequestParam("name")String name,
                                                         @RequestParam(value = "current",defaultValue = "1")int current,
                                                         @RequestParam(value = "size", defaultValue = "10")int size){
        return organUserService.queryOrganUserListByName(name,current,size);
    }



    /**
     * 插入一个成员
     * @param organUser
     * @return
     */
    @PostMapping("/insertOrganUser")
    @ResponseBody
    public ResponseResult insertOrganUser(@RequestBody OrganUser organUser){
        int result = organUserService.insertOrganUser(organUser);
        ResponsePage<OrganUser> res = organUserService.queryOrganUserListByNameAndOid(organUser.getOid(),organUser.getUname(),1,10);
        if(!res.getData().isEmpty() || res.getData().size() != 0){
            return ReponseResultUtil.error(-1, "请勿重复添加");
        }
        if (result == 0){
            return ReponseResultUtil.error(-1,"社团成员插入失败");
        }else{
            return ReponseResultUtil.success("社团成员插入成功");
        }
    }

    /**
     * 删除一个成员
     * @param
     * @return
     */
    @GetMapping("/deleteOrganUser")
    @ResponseBody
    public ResponseResult deleteOrganUser(@RequestParam("oid") int oid,
                                          @RequestParam("uid") String uid){
        int result = organUserService.deleteOrganUser(oid,uid);

        if (result == 0){
            return ReponseResultUtil.error(-1,"社团成员删除失败");
        }else{
            return ReponseResultUtil.success("社团成员删除成功");
        }

    }

    /**
     * 更新一个成员
     * @param organUser
     * @return
     */
    @PostMapping("/updateOrganUser")
    @ResponseBody
    public ResponseResult updateOrganUser(@RequestBody OrganUser organUser){
        int result = organUserService.updateOrganUser(organUser);
        if (result == 0){
            return ReponseResultUtil.error(-1,"社团成员更新失败");
        }else{
            return ReponseResultUtil.success("社团成员更新成功");
        }
    }

    @GetMapping("/exportMember")
    @ResponseBody
    public List<OrganUser> exportMember(){
        return organUserService.exportMember();
    }

}
